package leetcode.day;

// 747. 至少是其他数字两倍的最大数
public class DominantIndex {
    public static void main(String[] args) {
        int[] data = {3, 6, 1, 0, 99, 98};
        System.out.println(dominantIndex2(data));
    }

    public static int dominantIndex(int[] nums) {

        boolean flag = true;
        int max = nums[0];
        int index = 0;
        for (int i = 1; i < nums.length; i++) {
            if (nums[i] > max) {
                max = nums[i];
                index = i;
            }
        }
        for (int i = 0; i < nums.length; i++) {
            if (i != index) {
                if (max < nums[i] * 2) {
                    flag = false;
                    break;
                }
            }
        }
        if (flag) {
            return index;
        } else {
            return -1;
        }
    }

    // 只要最大值大于次大值的两倍，那么便是成功
    public static int dominantIndex2(int[] nums) {
        int index1 = 0, index2 = 0;
        int max = 0, less = 0;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] > max) {
                less = max;
                max = nums[i];
                index1 = i;
            } else if (nums[i] > less) {
                less = nums[i];
                index2 = i;
            }
        }
        System.out.println(index1);
        System.out.println(index2);
        return max >= (less * 2) ? index1 : -1;
    }


}
